<template>
	<view>
		<!-- 头部导航栏 -->
		<view style="display: flex;justify-content: space-between;align-items: center;padding: 28rpx 20rpx;position:fixed;top:0;left:0;right:0;height: 60px;z-index: 22;background-color: white;">
			<view style="display: flex;position: absolute;z-index: 22;" @click="back">
				<u-icon name="arrow-left"></u-icon>
				<view>返回</view>
			</view>
			
			<view style="font-weight: bold;position: absolute;left: 0;right: 0;height: 100%;display: flex;align-items: center;justify-content: center;">
				<view style="margin-left: 10rpx;">
					选择群发图文消息
				</view>
				
			</view>
<!-- 			<view @click="createNews" style="display: flex;align-items: center;;padding: 10rpx 25rpx;margin: 0 10rpx;border-radius: 10rpx;box-shadow: 0 0 14px 0 #66666635;border: solid 1rpx #ddd;">
				<u-icon name="plus"></u-icon>
				<view style="margin-left: 10rpx;">
					新建消息
				</view>
			</view> -->
		</view>
		
		<view style="margin: 60px 10px 10px 10px;padding-top: 10rpx;">
			<view v-for="(news,index) in newses">
				<view style="border-radius: 5px;overflow: hidden;margin: 20rpx 0 70rpx 0;box-shadow: 0 0 19px 1px #00000045;">
					<view style="margin: 0 20rpx;">
						<view style="display:flex;align-items: center;justify-content:space-between;margin: 20rpx 5rpx 10rpx 5rpx;font-size: 13px;">
							
							<view style="display: flex;">
								<view style="color: #888;">{{'上次发表:' + $dayjs(news.created_at).format('MM-DD HH:mm') }}</view>
								<view style="color: #888;">{{`(${$dayjs(news.created_at).fromNow()})`}}</view>
								<!-- <view style="color: #888;margin-left: 20rpx;">{{'桔赞'}}</view> -->
							</view>
							<view @click="()=>{showComfirm=true;selectedNews=news}" style="display: flex;align-items: center;padding: 10rpx 25rpx;margin: 0 0rpx;border-radius: 10rpx;box-shadow: 0 0 14px 0 #66666635;border: solid 1rpx #ddd;">
								<u-icon name="play-right-fill" color="#00c110"></u-icon>
								<view style="margin-left: 10rpx;">
									{{news.send_count > 0? `已群发(${news.send_count})次`:'群发'}}
								</view>
							</view>
						</view>
						<view style="display:flex;align-items: flex-end;justify-content:space-between;margin: 10rpx 5rpx 20rpx 5rpx;font-size: 13px;">
							<view style="color: #888;">{{'创建日期:' + $dayjs(news.created_at).format('YYYY-MM-DD HH:mm')}}</view>
							<view style="color: #888;">{{'次数:' + 5}}</view>
						</view>
					</view>
						
					<view @click="onPost(news.posts[0])" style="height: 150px;overflow: hidden;display: flex;align-items: center;position: relative;">
						<image style="width: 100%;" mode="widthFix" :src="absUrl(news.posts[0].thumb_url)"></image>
						
						<view style="display: flex;align-items: center;;position: absolute;left: 0;right: 0;top: 60%;bottom: 0;background-image: linear-gradient(0deg,#212121cc,#00000000);padding: 14rpx;overflow: hidden;">
							<view style="color: white;font-weight: bold;font-size: 16px">
								{{news.posts[0].title}}
							</view>
						</view>
					</view>
					
					<template v-if="news.posts.length > 0" >
						<template v-for="(post,i) in news.posts">
							<view v-if="i> 0" @click="onPost(post)" style="display: flex;padding: 20rpx 20rpx;border-bottom: solid 1rpx #eee;">
								<view style="flex: 1;">
									<view style="font-weight: bold;font-size: 15px;padding-bottom: 10px;">{{post.title}}</view>
									<!-- <view style="color: #888;">{{post.digest}}</view> -->
								</view>
								<view style="overflow: hidden;">
									<image style="width: 60px;height: 60px;" mode="aspectFill" :src="absUrl(post.thumb_url)"></image>
								</view>
							</view>
						</template>
					</template>
				</view>
			</view>
		</view>
	</view>
	
	<u-modal 
		:show="showComfirm" 
		showCancelButton
		@close="showComfirm=false" 
		@cancel="showComfirm=false"
		@confirm="()=>{showComfirm=false;send(selectedNews)}"
		title="是否确认群发?"
	>
	</u-modal>
</template>

<script>
	export default {
		async onLoad(options){
			// let res = this.$localStorage.getItem('posts') 
			
			// if(!res)
			// {
			// 	 res = await this.$http.get('/common/wx/get_published_list')
			// 	 this.$localStorage.setItem('posts',res)
			// }
			
			// this.$officePost.drafts = res.data.item

			console.log(options)
			
			this.office_id = options.office_id
			
			let res = await this.$http.get(`/common/wx/news?office_id=${options.office_id}`)
			
			
			this.newses = res.data
		},
		data() {
			return {
				office_id:-1,
				newses:[],
				showComfirm:false,
				selectedNews:null
			}
		},
		methods: {
			back(){
				uni.navigateBack()
			},
			onPost(post){
				console.log('................')
				uni.navigateTo({
					url: `/pages/job/publish/job-offices/send-office-msg/post-send-preview?post_id=${post.id}`,
				})
			},
			createNews(){
				// uni.navigateTo({
				// 	url: '/pages/job/publish/job-offices/send-office-msg/select-post-draft'
				// })
				
				//新建消息跳到新建板块
			},
			async send(news)
			{
				console.log(news)
				
				uni.showLoading({
					title:'正在群发'
				})
				
				let  result = await this.$http.get(`/common/wx/send_news?office_id=${this.office_id}&news_id=${news.id}`)
				
				uni.hideLoading()
				
				setTimeout(()=>{
					uni.showToast({
						title:'群发成功'
					})
				},500)
			}
		}
	}
</script>

<style>

</style>
